import { ProTableColumn } from '@/components/pro-table/type'
import { NButton, NPopconfirm, NTag } from 'naive-ui'
import { BorrowModel } from '@/api/model/borrow-model'
import { Ref } from 'vue'
import { userInfo } from '~/types/user'
import { userInfoModel } from '@/api/model/userModel'

type Props = {
  handleEditBorrow: (row: BorrowModel, type: 'edit' | 'check') => void
  fetchDelBorrow: (id: number) => void
  typeOptions: Ref<any>
  typeLoading: Ref<boolean>
  userInfo: Ref<userInfo<userInfoModel>>
}

export const useBorrowProTable = ({
  handleEditBorrow,
  fetchDelBorrow,
  typeOptions,
  typeLoading,
  userInfo
}: Props) => {
  const showEdit = (row: any) => {
    // 申请未全部通过 且不为本部门借出
    return (
      (row.borrowOutStatus !== '2' || row.borrowInStatus !== '2') &&
      row.borrowOutBranchId !== userInfo.value.orgId.toString()
    )
  }

  const showDetail = (row: any) => {
    // 申请全部通过或者为本部门借出
    return (
      (row.borrowOutStatus === '2' && row.borrowInStatus === '2') ||
      row.borrowOutBranchId === userInfo.value.orgId.toString()
    )
  }
  // 申请未全部通过 且不为本部门借出
  const showDel = (row: any) => {
    return (
      (row.borrowOutStatus !== '2' && row.borrowInStatus !== '2') ||
      row.borrowOutBranchId !== userInfo.value.orgId.toString()
    )
  }

  const columns = computed<ProTableColumn<BorrowModel>[]>(() => {
    return [
      {
        title: '借用编号',
        key: 'borrowCode',
        query: {
          type: 'input'
        }
      },
      {
        title: '借用名称',
        key: 'borrowName',
        query: {
          type: 'input'
        }
      },
      {
        title: '借用资产名称',
        key: 'assetName'
        // query: {
        //   type: 'input'
        // }
      },
      {
        type: 'dict',
        key: 'typeId',
        title: '借用资产类型',
        dictOptions: typeOptions.value
        // query: {
        //   type: 'select',
        //   dictOptions: typeOptions.value
        // }
      },
      {
        title: '借入部门',
        key: 'borrowInBranch',
        query: {
          type: 'input'
        }
      },
      {
        title: '借出部门',
        key: 'borrowOutBranch',
        query: {
          type: 'input'
        }
      },
      {
        type: 'tag',
        title: '借入状态',
        key: 'borrowInStatus',
        dictOptions: useDictData('STATUS').value,
        query: {
          type: 'select',
          dictOptions: useDictData('STATUS').value
        }
      },
      {
        type: 'tag',
        title: '借出状态',
        key: 'borrowOutStatus',
        dictOptions: useDictData('STATUS').value,
        query: {
          type: 'select',
          dictOptions: useDictData('STATUS').value
        }
      },
      // {
      //   title: '借用资产编码',
      //   key: 'assetCode'
      // },
      {
        title: '申请人',
        key: 'receivePerson',
        query: {
          type: 'input'
        }
      },
      // {
      //   title: '申请部门',
      //   key: 'receiveBranch'
      // },
      {
        title: '申请日期',
        key: 'receiveDate'
      },
      // {
      //   title: '借用原因',
      //   key: 'receiveReason'
      // },
      // {
      //   type: 'dict',
      //   key: 'borrowType',
      //   title: '借用类型',
      //   dictOptions: useDictData('ALLOCATE_TYPE').value
      // },
      {
        key: 'borrowType',
        title: '借用类型',
        // dictOptions: useDictData('ALLOCATE_TYPE').value
        render(row) {
          return (
            <>
              {row.borrowInBranchId === userInfo.value.orgId ? (
                <NTag type="info">借入</NTag>
              ) : (
                <NTag type="warning">借出</NTag>
              )}
            </>
          )
        }
      },
      // {
      //   title: '备注',
      //   key: 'remark'
      // },
      {
        title: '操作',
        key: 'operation',
        render(row) {
          return (
            <>
              {showEdit(row) ? (
                <NButton
                  type="primary"
                  size="small"
                  onClick={() => handleEditBorrow(row, 'edit')}
                  quaternary
                >
                  修改
                </NButton>
              ) : (
                ''
              )}

              {showDetail(row) ? (
                <NButton
                  type="primary"
                  size="small"
                  onClick={() => handleEditBorrow(row, 'check')}
                  quaternary
                >
                  详情
                </NButton>
              ) : (
                ''
              )}

              {showDel(row) ? (
                <NPopconfirm onPositiveClick={() => fetchDelBorrow(row.id)}>
                  {{
                    trigger: () => (
                      <NButton size="small" type="error" quaternary>
                        删除
                      </NButton>
                    ),
                    default: () => '您确定要删除？'
                  }}
                </NPopconfirm>
              ) : (
                ''
              )}
            </>
          )
        }
      }
    ]
  })

  return {
    columns
  }
}
